package com.itheima.app.manager.omsManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.oms.OmsCompanyAddress;
import com.itheima.domain.db.oms.OmsOrderReturnApply;
import com.itheima.dto.OmsOrderReturnApplyDto;
import com.itheima.service.omsService.CompanyAddressService;
import com.itheima.service.omsService.OrderReturnApplyService;
import com.itheima.vo.OmsReturnApplyVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 退货申请
 *
 * @author DogeGuo
 */
@Component
public class OmsOrderReturnApplyManager {
    @DubboReference
    OrderReturnApplyService orderReturnApplyService;

    /**
     * 分页查询退货申请
     *
     * @param dto
     * @param pageNum
     * @param pageSize
     * @return
     */
    public R pageReturnWithParams(OmsOrderReturnApplyDto dto, Integer pageNum, Integer pageSize) {
        PageBean<OmsOrderReturnApply> pageBean = orderReturnApplyService.pageReturnWithParams(dto, pageNum, pageSize);
        return R.ok(pageBean);
    }

    @DubboReference
    CompanyAddressService companyAddressService;

    /**
     * 获取退货申请详情
     *
     * @param id
     * @return
     */
    public OmsReturnApplyVo getInfoById(Integer id) {
        OmsReturnApplyVo vo = new OmsReturnApplyVo();
        //根据id查询退货表
        OmsOrderReturnApply omsOrderReturnApply = orderReturnApplyService.getInfoById(id);
        BeanUtil.copyProperties(omsOrderReturnApply, vo);

        //根据地址id查询退货地址
        Long companyAddressId = omsOrderReturnApply.getCompanyAddressId();
        OmsCompanyAddress omsCompanyAddress = companyAddressService.findById(companyAddressId);
        vo.setCompanyAddress(omsCompanyAddress);
        return vo;
    }

    /**
     * 修改退货申请状态
     *
     * @param id
     * @param omsOrderReturnApply
     * @return
     */
    public void updateStatusById(Integer id, OmsOrderReturnApply omsOrderReturnApply) {
        //保存id
        omsOrderReturnApply.setId(Convert.toLong(id));
        orderReturnApplyService.updateStatusById(omsOrderReturnApply);
    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     */
    public void deleteByIds(Integer[] ids) {
        orderReturnApplyService.deleteByIds(ids);
    }
}
